package com.itheima.web; /**
 * @author tongchen
 * @create 2022-11-29 14:23
 */

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
//import com.itheima.util.SqlSessionFactoryUtils;
import com.itheima.utils.SessionnFactroyUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //接收前端的返回值
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //2.1创建factory对象
        SqlSessionFactory sqlSessionFactory = SessionnFactroyUtils.getFactory();
        //2.2创建sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.3获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //2.4调用方法
        User user = mapper.select(username, password);
        //2.5释放资源
        sqlSession.close();
        //设置字符集
        response.setContentType("text/html;charset=utf-8");
        //创建写入方法
        PrintWriter writer = response.getWriter();
        if(user==null){
            writer.write("登录失败");
        }
        else{
            writer.write("登录成功");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
